package repository

import (
	"django_blog_gin/internal/dto"
	"django_blog_gin/internal/model"
	"django_blog_gin/internal/svc"
	"fmt"
)

type CategoryRepo struct {
	svcCtx *svc.ServiceContext
}

func NewCategoryRepo(svcCtx *svc.ServiceContext) *CategoryRepo {
	return &CategoryRepo{svcCtx: svcCtx}
}
func (i *CategoryRepo) GetCategoryTotal(name string) (int, error) {
	var total int
	sql := fmt.Sprintf(`
        SELECT 
            COUNT(*)
        FROM %s
    `, model.BlogCategory{}.TableName())
	var args []interface{}
	if name != "" {
		sql += " WHERE name LIKE ?"
		args = append(args, "%"+name+"%")
	}
	err := i.svcCtx.MysqlDb.Get(&total, sql, args...)
	if err != nil {
		return 0, err
	}
	return total, nil
}

// 获取所有的分类（然后再根据parent进行分类处理)
func (i *CategoryRepo) GetAllCategory() ([]*model.BlogCategory, error) {
	categoryList := make([]*model.BlogCategory, 0)
	sql := fmt.Sprintf("SELECT id, created_time, last_mod_time, name, slug, `index`, parent_category_id FROM %s", model.BlogCategory{}.TableName())
	err := i.svcCtx.MysqlDb.Select(&categoryList, sql)
	if err != nil {
		return nil, err
	}
	return categoryList, nil
}

func (i *CategoryRepo) GetCategoryList(limit string, offset string, name string) ([]*model.BlogCategory, error) {
	categoryList := make([]*model.BlogCategory, 0)
	sql := fmt.Sprintf("SELECT id, created_time, last_mod_time, name, slug, `index`, parent_category_id FROM %s", model.BlogCategory{}.TableName())
	var args []interface{}
	if name != "" {
		sql += " WHERE name LIKE ?"
		args = append(args, "%"+name+"%")
	}
	sql += " LIMIT ? OFFSET ?"
	args = append(args, limit, offset)
	err := i.svcCtx.MysqlDb.Select(&categoryList, sql, args...)
	if err != nil {
		return nil, err
	}
	return categoryList, nil
}

func (i *CategoryRepo) CheckCategoryExists(name string) (int, error) {
	var count int
	sql := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE name = ?", model.BlogCategory{}.TableName())
	err := i.svcCtx.MysqlDb.Get(&count, sql, name)
	if err != nil {
		return 0, err
	}
	return count, nil
}

func (i *CategoryRepo) CreateCategory(category *dto.CreateCategoryReq) error {
	sql := fmt.Sprintf("INSERT INTO %s (name, slug, `index`, parent_category_id, created_time, last_mod_time) VALUES (:name, :slug, :index, :parent_category_id, NOW(), NOW())", model.BlogCategory{}.TableName())
	_, err := i.svcCtx.MysqlDb.NamedExec(sql, category)
	if err != nil {
		return err
	}
	return nil
}

func (i *CategoryRepo) UpdateCategory(category *dto.UpdateCategoryReq) error {
	sql := fmt.Sprintf("UPDATE %s SET name = :name, slug = :slug, `index` = :index, parent_category_id = :parent_category_id, last_mod_time = NOW() WHERE id = :id", model.BlogCategory{}.TableName())
	_, err := i.svcCtx.MysqlDb.NamedExec(sql, category)
	if err != nil {
		return err
	}
	return nil
}

func (i *CategoryRepo) SearchCategoryWithId(id int) (*model.BlogCategory, error) {
	categoryDetail := model.BlogCategory{}
	CategoryDetailSql := fmt.Sprintf(" SELECT id, name, slug, `index`, parent_category_id FROM %s WHERE id = ?", model.BlogCategory{}.TableName())
	err := i.svcCtx.MysqlDb.Get(&categoryDetail, CategoryDetailSql, id)
	if err != nil {
		return nil, err
	}
	return &categoryDetail, nil

}
